// pages/product/product.js
import { Product } from 'product-model.js';
import { Cart } from '../cart/cart-model.js';
var product = new Product();
var cart = new Cart();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    loadingHidden: false,
    hiddenSmallImg: true,
    countsArray: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
    productCounts: 1,
    currentTabsIndex: 0,
    cartTotalCounts: 0,
    isFly: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var id = options.id;
    console.log(id);
    this.data.id = id;
    this._loadData();
  },

  /**
   * 加载所有数据
   */
  _loadData: function (callBack) {
    var that = this;
    product.getDetailInfo(this.data.id, (data) => {
      that.setData({
        cartTotalCounts:cart.getCartTotalCounts().counts1, // 购物车初始数量
        product: data, // 商品数据
        loadingHidden: true // 加载消失
      });
      callBack && callBack();
    });
  },

  /**
   * 选择购买条目
   */
  bindPickerChange: function (e) {
    var productCounts = this.data.countsArray[e.detail.value];
    this.setData({
      productCounts: productCounts
    });
  },

  /**
   * 切换详情面板
   */
  onTabsItemTap: function (event) {
    var index = product.getDataSet(event, 'index');
    this.setData({
      currentTabsIndex: index
    });
    console.log(event);
  },

  onAddingToCartTap: function (e) {
    // 防止快速点击
    if (this.data.isFly) {
      return;
    }
    this._flyToCartEffect(e);
    this.addToCart();
  },

  /**
   * 将购物车数据存入到缓存中
   */
  addToCart:function(){
    var tempObj = {},keys=['id','name','main_img_url','price'];
    for(var key in this.data.product){
      if(keys.indexOf(key) >= 0){
        tempObj[key] = this.data.product[key];
      }
    }
    cart.add(tempObj,this.data.productCounts);
  },

  /*加入购物车动效*/
  _flyToCartEffect: function (events) {
    //获得当前点击的位置，距离可视区域左上角
    var touches = events.touches[0];
    var diff = {
      x: '25px',
      y: 25 - touches.clientY + 'px'
    };
    var style = 'display: block;-webkit-transform:translate(' + diff.x + ',' + diff.y + ') rotate(350deg) scale(0)';  //移动距离
    this.setData({
      isFly: true,
      translateStyle: style
    });
    var that = this;
    setTimeout(() => {
      that.setData({
        isFly: false,
        translateStyle: '-webkit-transform: none;',  //恢复到最初状态
        isShake: true,
      });
      setTimeout(() => {
        var counts = that.data.cartTotalCounts + that.data.productCounts;
        that.setData({
          isShake: false,
          cartTotalCounts: counts
        });
      }, 200);
    }, 1000);
  },

  /**
   * 跳转到购物车
   */
  onCartTap:function(){
    wx.switchTab({
      url: '/pages/cart/cart',
    })
  },

  /**
   * 下拉刷新
   */
  onPullDownRefresh:function(){
    this._loadData(()=>{
      wx.stopPullDownRefresh();
    });
  }

})